﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kunlun.Models;
using Kunlun.BusinessLogic;
namespace Kunlun.Controllers
{
     [HandleError]
    public class MyPhotoController : Controller
    {
        //
        // GET: /MyPhoto/
        KunLunEntities entities = new KunLunEntities();
        Kunlunphotolib libs = new Kunlunphotolib();
        KunlunUploadFile upload = new KunlunUploadFile();
        [Authorize]       
        public ActionResult Index()
        {          
            
             int userid = Convert.ToInt32(Request.Cookies.Get("userId").Value);
             var photolib = entities.Consumer_library.Where(p => p.UserID == userid&&p.IsVisible==true).OrderByDescending(p=>p.Orders).Take(20).ToList();
             return View(photolib);
        }
        [Authorize]
        public ActionResult MyPhotoEdit(int id)
        {
            int userid = Convert.ToInt32(Request.Cookies.Get("userId").Value);
            var photolib = entities.Consumer_library.Single(p=>p.UserID==userid&&p.ID==id);
            return View(photolib);
            
        }   
         [HttpPost]      
        public ActionResult MyPhotoEdit(Consumer_library lib,int id)
        {

            
            if (ModelState.IsValid)
            {
                lib.ID = id;
                libs.ModifyLib(lib);
            }
            return  RedirectToAction("Index");
        }
         public ActionResult MyPhotoAdd()
         {
             return View();
         }
         [HttpPost]
         public ActionResult MyPhotoAdd(Consumer_library lib)
         {
             int userid = Convert.ToInt32(Request.Cookies.Get("userId").Value);
             libs.AddLib(lib, userid);
             return RedirectToAction("Index");
         }
         public ActionResult UploadFile(int id)
         {
             ViewBag.libid = id;
             return View();
         }
         [HttpPost]
         public void UploadFile(FormCollection collection)
         {
             string id = Request.QueryString[0];
             HttpFileCollectionBase file = Request.Files;
             for (int i = 0; i < file.Count; i++)
             {
                 upload.SaveFile(file[i], id,Request.UserHostAddress);
                 
             }
         }
         public ActionResult MyPhotoList(int? page,int? id)
         {
             PageModel model=new PageModel();
             
             if (page == null||page==0)
             {
                 page = 1;
             }
             int pageindex = Convert.ToInt32(page-1);
             int userid = Convert.ToInt32(Request.Cookies.Get("userId").Value);
             var pic = from p in entities.Consumer_pic
                       where p.UserID == userid && p.Status==1                       
                       orderby p.ID
                       select p;

             ViewModels.ViewConsumer_picViewModels viewpic = new ViewModels.ViewConsumer_picViewModels();
           
             
             model.PageIndex=pageindex;
             model.PageSize=20;

             if (id != null)
             {
                 int libid = Convert.ToInt32(id);
                 pic = pic.Where(p => p.Library == libid).OrderBy(p => p.ID);
                 model.ID=id.ToString();
             }
             viewpic.Pic = pic.Skip(pageindex * model.PageSize).Take(model.PageSize).ToList();
             
             model.ItemCount = pic.Count();
             ViewBag.Page = model;
             return View(viewpic);
             
         }
         [HttpPost]
         public ActionResult MyPhotoDel(int id)
         {
             libs.DeleLib(id);
             return Json(true, JsonRequestBehavior.AllowGet);
         }
         [HttpPost]
         public ActionResult MyPicDel(int id)
         {
             Consumer_pic pic = entities.Consumer_pic.Single(p => p.ID == id);
             pic.Status = 0;
             entities.Entry(pic).State = EntityState.Modified;
             entities.SaveChanges();
             return Json(true, JsonRequestBehavior.AllowGet);
         }
    }
}
